package cn.donting.jme.edit.ex.imp;

import cn.donting.jme.edit.ex.IconResourcesLoader;
import com.jme3.light.Light;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.net.URL;

public class DefaultIconResourcesLoader implements IconResourcesLoader {
    @Override
    public ImageView getIcon(Spatial spatial) {
        Image iconImage = getIconImage(spatial);
        if(iconImage!=null){
            return new ImageView(iconImage);
        }
        return null;
    }

    @Override
    public Image getIconImage(Spatial spatial) {
        if (spatial instanceof Node) {
            return getImage("icon/node.png");
        }
        if (spatial instanceof Geometry) {
            return getImage("icon/geometry.png");
        }
        return null;
    }

    @Override
    public ImageView getIcon(Light spatial) {
        return null;
    }

    private Image getImage(String name) {
        ClassLoader classLoader = DefaultIconResourcesLoader.class.getClassLoader();
        URL resource = classLoader.getResource(name);
        Image image = new Image(resource.toString());
        return image;
    }
}
